package 二叉搜索树;

public class BinarySearch {
    public TreeNode root;
    public int count=0;
    //添加节点
    public void add(int data){
        TreeNode newNode=new TreeNode(data);
        if(root==null){
            root=newNode;
            return;
        }else{
            TreeNode cur=root;
            while(true){
                if(newNode.value<cur.value){
                    if(cur.left==null){
                        cur.left=newNode;
                        return;
                    }else{
                        cur=cur.left;
                    }
                }else{
                    if(cur.right==null){
                        cur.right=newNode;
                        return;
                    }else{
                        cur=cur.right;
                    }
                }
            }
        }
    }
    //查找该二叉树上是否含有该元素
    public boolean findTreeNode(TreeNode root,int value){
        if(root==null){
            return false;
        }else{
            if(root.value==value){
                return true;
            }else{
                if(value<root.value){
                    count++;
                    return findTreeNode(root.left,value);
                }else{
                    count++;
                    return findTreeNode(root.right,value);
                }
            }
        }
    }

}
